package org.ee.core.api;

import io.swagger.v3.oas.annotations.media.Schema;
import org.ee.core.constant.ResultCode;

/**
 * 通用返回结果
 *
 * @Author jokr
 * @Date 2025/8/18 下午3:43
 */
@Schema(description = "通用返回结果")
public class Result<T> {
    @Schema(description = "结果代码")
    private Integer code;
    @Schema(description = "结果信息")
    private String msg;
    @Schema(description = "结果数据")
    private T data;

    public Result() {
    }

    public Result(ResultCode resultCode, T Data) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMessage();
        this.data = Data;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功
     */
    public static <T> Result<T> success() {
        return new Result<>(ResultCode.SUCCESS, null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS, data);
    }

    public static <T> Result<T> success(String msg, T data) {
        Result<T> r = new Result<>(ResultCode.SUCCESS, data);
        r.setMsg(msg);
        return r;
    }

    /**
     * 失败
     */
    public static <T> Result<T> failure(ResultCode resultCode) {
        return new Result<>(resultCode, null);
    }

    public static <T> Result<T> failure(ResultCode resultCode, T data) {
        return new Result<>(resultCode, data);
    }

    public static <T> Result<T> failure(String msg) {
        Result<T> r = new Result<>(ResultCode.FAILURE, null);
        r.setMsg(msg);
        return r;
    }

    /**
     * 新增：自定义 code + message
     *
     * @param code
     * @param msg
     * @return
     */
    public static Result failure(Integer code, String msg) {

        return new Result<>(code, msg, null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
